package com.ebt.m.customer.util;

import java.util.ArrayList;

import android.text.TextUtils;

public class PinyinUtil {

	public static String space = " ";
    public static String semicolon = ";";
    
    //不考虑多音字
    public static String generateDisplayStringSingle(String oriLabel){

    	char[] charArray = oriLabel.toCharArray();
    	int labelLength = charArray.length;
		char key;
    	String value;
		StringBuilder buffer = new StringBuilder();
		
		boolean addSpace = false;
		
    	for(int i = 0; i < labelLength; i++){ 	
    		key = charArray[i];
    		HanziToPinyin.Token tokens = HanziToPinyin.getInstance().getToken(key);
    		
			if(tokens.type == HanziToPinyin.Token.PINYIN){
				value = tokens.target.toUpperCase();
				if(addSpace){
					buffer.append(space);
				}
				buffer.append(value).append(space).append(key).append(space);
				addSpace = false;
				
			} else if(tokens.type == HanziToPinyin.Token.LATIN){	//ONLY one char
				addSpace = true;
				buffer.append(key);
			}
    	}
    	return buffer.toString();
    }
 
    //多音字
    public static String generateDisplayStringMultiple(String oriLabel){

    	StringBuilder sbForPostConcat = new StringBuilder();
    	ArrayList<String> result = new ArrayList<String>();
    	char[] charArray = oriLabel.toCharArray();
    	int labelLength = charArray.length;

		char key;
    	String value;
		
		boolean addSpace = false;

    	for(int i = 0; i < labelLength; i++){ 	
    		key = charArray[i];
    		HanziToPinyin.Token tokens = HanziToPinyin.getInstance().getToken(key);
    		
			if(tokens.type == HanziToPinyin.Token.PINYIN){
				
				int codeindex = oriLabel.codePointAt(i);
				boolean isPolyphone = HanziToPinyin.name_to_pinyin.containsKey(codeindex);

				if(isPolyphone){
					String pinyinsStr = HanziToPinyin.name_to_pinyin.get(codeindex);//s 是多个拼音，使用","分割
					result = appendString_DuoYinZi(result, pinyinsStr, key);
				}else{
					value = tokens.target.toUpperCase();

					sbForPostConcat.delete(0, sbForPostConcat.length());
					
					if(addSpace){
						sbForPostConcat.append(space);
					}
					sbForPostConcat.append(value).append(space).append(key).append(space);
					result = appendString_DanYinZi(result, sbForPostConcat.toString());
				}
				addSpace = false;
				
			} else if(tokens.type == HanziToPinyin.Token.LATIN){	//ONLY one char
				
				result = appendString_DanYinZi(result, String.valueOf(key));
				addSpace = true;
				
			}
    	}
    	
    	if(result.size() == 1){
    		return result.get(0);	//单音不加";"
    	}else if(result.size() > 1){
    		StringBuilder buffer = new StringBuilder();
    		for(String item : result){
    			buffer.append(item).append(semicolon).append(space);
    		}
    		return buffer.toString();
    	}else{
    		return "#";
    	}
    }
    
    
    public static ArrayList<String> appendString_DanYinZi(ArrayList<String> input, String postStr){
    	int size = input.size();
		if(input.size() > 0){
			for(int i = 0; i < size; i++){
				input.set(i, input.get(i).concat(postStr));
			}
		}else{
			input.add(postStr);
		}
	    return input;
    }
    

    public static ArrayList<String> appendString_DuoYinZi(ArrayList<String> input, String str, char hanzi){
		String[] pinyins = str.split(",");
		ArrayList<String> output = new ArrayList<String>();
		
		if(input.size() > 0){
			for(String item : input){
				for(String pinyin : pinyins){
					output.add(item + space + pinyin.toUpperCase() + space + hanzi + space);
				}
			}
		}else{
			for(String pinyin : pinyins){
				output.add(pinyin.toUpperCase() + space + hanzi + space);
	    	}
		}
	    input = null;
	    return output;
    }
    
    public static char getSortKeyChar(String sortKeyString) {
		if(!TextUtils.isEmpty(sortKeyString)){
			char c = sortKeyString.charAt(0);
			if(c >= 97 && c <= 122){
				return (char) (c - 32);
			}
			if(c >= 65 && c <= 90){
				return c;
			}
		}
		return '#';
	}
	
}
